package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.GoodsService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    private final GoodsService goodsService;

    public GoodsController(GoodsService goodsService){
        this.goodsService = goodsService;
    }


    /**
     * 商品基本查询
     * @param rows
     * @param page
     * @param brandId
     * @param categoryId
     * @param saleable
     * @param id
     * @return
     */
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> pageQuery(
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "brandId",required = false)Long brandId,
            @RequestParam(value = "categoryId",required = false)Long categoryId,
            @RequestParam(value = "saleable",required = false)Boolean saleable,
            @RequestParam(value = "id",required = false)Long id){

        return ResponseEntity.ok(this.goodsService.pageQuery(rows,page,brandId,categoryId,saleable,id));
    }




    /**
     * 根据spuId查询对应的sku集合
     * @param spuId
     * @return
     */
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> listSkuBySpu(@RequestParam("id")Long spuId){

        return ResponseEntity.ok(this.goodsService.listSkuBySpu(spuId));
    }


    /**
     * 根据sku的id集合查询对应的sku集合
     * @param ids
     * @return
     */
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> listSkuByIds(@RequestParam("ids")List<Long> ids){

        return ResponseEntity.ok(this.goodsService.listSkuByIds(ids));
    }


    /**
     * 根据商品id查询对应的商品详情
     * @param spuId
     * @return
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> findSpuDetailBySpu(@RequestParam("id")Long spuId){

        return ResponseEntity.ok(this.goodsService.findSpuDetailBySpu(spuId));
    }



    /**
     * 根据商品id查询对应的spu信息
     * @param id
     * @return
     */
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> findSpuById(@PathVariable("id")Long id){

        return ResponseEntity.ok(this.goodsService.findSpuById(id));
    }

    /**
     * 完整的商品查询，要查询商品的所有信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<SpuDTO> findGoodsById(@PathVariable("id")Long id){
        return ResponseEntity.ok(this.goodsService.findGoodsById(id));
    }

    /**
     * 根据商品(spu)id查询其对应的所有的规格参数以及参数的值
     * @param spuId
     * @param searching
     * @return
     */
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> findSpecParamWithValueBySpu(
            @RequestParam("id")Long spuId,
            @RequestParam(value = "searching",required = false)Boolean searching){

        return ResponseEntity.ok(this.goodsService.findSpecParamWithValueBySpu(spuId,searching));
    }


    @PostMapping("/spu")
    public ResponseEntity<Void> addGoods(
            @RequestBody SpuDTO spuDTO){

        this.goodsService.addGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }


    @PutMapping("/saleable")
    public ResponseEntity<Void> modifySaleable(
            @RequestParam("id")Long id,
            @RequestParam("saleable")Boolean saleable
    ){
        this.goodsService.modifySaleable(id,saleable);
        return ResponseEntity.ok().build();
    }

}
